package com.glsc.ngateway.common.base.repo.mysql.gateway.platform;


import com.glsc.ngateway.common.api.flowable.dto.EventStatisticParamDto;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.flow.FlowFormEvent;
import com.glsc.ngateway.common.api.flowable.dto.FlowParamAuthDto;
import com.glsc.ngateway.common.base.domain.mysql.gateway.system.VEhrAdminStaff;
import feign.Param;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

@Repository
public interface FlowFormEventRepo extends JpaRepository<FlowFormEvent, String>, JpaSpecificationExecutor<FlowFormEvent> {

    //根据流程Id检索表单
    FlowFormEvent findByProcessId(String processId);

    @Query(value = "select f from FlowFormEvent f where f.isClose != 2 and (:start is null or f.processCreateTime >= :start)" +
            " and (:end is null or f.processCreateTime <= :end) " +
            " and (:#{#param.happenBeginDate} is null OR f.eventHappenTime >= :#{#param.happenBeginDate} )  " +
            " and (:#{#param.happenEndDate} is null OR f.eventHappenTime <= :#{#param.happenEndDate} )  " +
            " and (:flowCreateOperatorAccount is null or f.flowCreateOperatorAccount = :flowCreateOperatorAccount) " +
            " and ( COALESCE(:eventCause, null) IS NULL OR f.eventCause IN :eventCause) " +
            "  ")
    List<FlowFormEvent> eventStatistics(@Param("start") LocalDateTime start, @Param("end") LocalDateTime end, @Param("flowCreateOperatorAccount") String flowCreateOperatorAccount, @Param("eventCause") List<Integer> eventCause, @Param("param") EventStatisticParamDto param);

    @Query(value = "select f from FlowFormEvent f where f.isClose != 2 and (:start is null or f.processCreateTime >= :start)" +
            " and (:end is null or f.processCreateTime <= :end) " +
            " and (:#{#param.happenBeginDate} is null OR f.eventHappenTime >= :#{#param.happenBeginDate} )  " +
            " and (:#{#param.happenEndDate} is null OR f.eventHappenTime <= :#{#param.happenEndDate} )  " +
            " and (:flowCreateOperatorAccount is null or f.flowCreateOperatorAccount = :flowCreateOperatorAccount) " +
            " and ( COALESCE(:eventCause, null) IS NULL OR f.eventCause IN :eventCause) " +
            "and (f.systemBelongDepartment in (:#{#authCondition.belongDepartment})  or f.systemCode in (:#{#authCondition.adminSystem})" +
            " or f.processId in (:#{#authCondition.processIds}) or f.flowCreateOperatorAccount in (:#{#authCondition.creatorEmps}) or f.systemAdminAccount in (:#{#authCondition.systemAdminEmps})  " +
            ")" +
            "  ")
    List<FlowFormEvent> eventStatisticsWith(@Param("start") LocalDateTime start, @Param("end") LocalDateTime end, @Param("flowCreateOperatorAccount") String flowCreateOperatorAccount, @Param("authCondition") FlowParamAuthDto authCondition, @Param("eventCause") List<Integer> eventCause, EventStatisticParamDto param);

    @Query(value = "select f from FlowFormEvent f where f.isClose != 2 and (:start is null or f.processCreateTime >= :start)" +
            " and (:end is null or f.processCreateTime <= :end) " +
            " and (:#{#param.happenBeginDate} is null OR f.eventHappenTime >= :#{#param.happenBeginDate} )  " +
            " and (:#{#param.happenEndDate} is null OR f.eventHappenTime <= :#{#param.happenEndDate} )  " +
            " and (:flowCreateOperatorAccount is null or f.flowCreateOperatorAccount = :flowCreateOperatorAccount) " +
            " and ( COALESCE(:eventCause, null) IS NULL OR f.eventCause IN :eventCause) " +
            "and (f.systemBelongDepartment in (:#{#authCondition.belongDepartment}) " +
            " or f.processId in (:#{#authCondition.processIds}) or f.flowCreateOperatorAccount in (:#{#authCondition.creatorEmps}) or f.systemAdminAccount in (:#{#authCondition.systemAdminEmps})  " +
            ")" +
            "  ")
    List<FlowFormEvent> eventStatisticsBelongDepartment(@Param("start") LocalDateTime start, @Param("end") LocalDateTime end, @Param("flowCreateOperatorAccount") String flowCreateOperatorAccount, @Param("authCondition") FlowParamAuthDto authCondition, @Param("eventCause") List<Integer> eventCause, EventStatisticParamDto param);

    @Query(value = "select f from FlowFormEvent f where f.isClose != 2 and (:start is null or f.processCreateTime >= :start)" +
            " and (:end is null or f.processCreateTime <= :end) " +
            " and (:#{#param.happenBeginDate} is null OR f.eventHappenTime >= :#{#param.happenBeginDate} )  " +
            " and (:#{#param.happenEndDate} is null OR f.eventHappenTime <= :#{#param.happenEndDate} )  " +
            " and (:flowCreateOperatorAccount is null or f.flowCreateOperatorAccount = :flowCreateOperatorAccount) " +
            " and ( COALESCE(:eventCause, null) IS NULL OR f.eventCause IN :eventCause) " +
            "and ( f.systemCode in (:#{#authCondition.adminSystem})" +
            " or f.processId in (:#{#authCondition.processIds}) or f.flowCreateOperatorAccount in (:#{#authCondition.creatorEmps}) or f.systemAdminAccount in (:#{#authCondition.systemAdminEmps})  " +
            ")" +
            "  ")
    List<FlowFormEvent> eventStatisticsAdminSystem(@Param("start") LocalDateTime start, @Param("end") LocalDateTime end, @Param("flowCreateOperatorAccount") String flowCreateOperatorAccount, @Param("authCondition") FlowParamAuthDto authCondition, @Param("eventCause") List<Integer> eventCause, EventStatisticParamDto param);

    @Query(value = "select f from FlowFormEvent f where f.isClose != 2 and (:start is null or f.processCreateTime >= :start)" +
            " and (:end is null or f.processCreateTime <= :end) " +
            " and (:#{#param.happenBeginDate} is null OR f.eventHappenTime >= :#{#param.happenBeginDate} )  " +
            " and (:#{#param.happenEndDate} is null OR f.eventHappenTime <= :#{#param.happenEndDate} )  " +
            " and (:flowCreateOperatorAccount is null or f.flowCreateOperatorAccount = :flowCreateOperatorAccount) " +
            " and ( COALESCE(:eventCause, null) IS NULL OR f.eventCause IN :eventCause) " +
            "and (" +
            "  f.processId in (:#{#authCondition.processIds}) or f.flowCreateOperatorAccount in (:#{#authCondition.creatorEmps}) or f.systemAdminAccount in (:#{#authCondition.systemAdminEmps})  " +
            ")" +
            "  ")
    List<FlowFormEvent> eventStatisticsCommon(@Param("start") LocalDateTime start, @Param("end") LocalDateTime end, @Param("flowCreateOperatorAccount") String flowCreateOperatorAccount, @Param("authCondition") FlowParamAuthDto authCondition, @Param("eventCause") List<Integer> eventCause, EventStatisticParamDto param);

    @Query(value = "select distinct * from t_gw_flow_form_event f " +
            " where f.is_close != 2 and (:start is null or f.process_create_time >= :start)" +
            " and (:end is null or f.process_create_time <= :end) " +
            " and (:#{#param.happenBeginDate} is null OR f.event_happen_time >= :#{#param.happenBeginDate} )  " +
            " and (:#{#param.happenEndDate} is null OR f.event_happen_time <= :#{#param.happenEndDate} )  " +
            " and (:flowCreateOperatorAccount is null or f.flow_create_operator_account = :flowCreateOperatorAccount) " +
            " and ( COALESCE(:eventCause, null) IS null OR f.event_cause IN (:eventCause)) " +
            " and (COALESCE(:#{#authCondition.admin}, null) IS null or " +
            " ( f.process_id IN (:#{#authCondition.processIds}) " +
            "or f.system_code in (select a.system_class_no from t_gw_system_member  m inner join t_gw_system_main a on m.op_id = a.op_id where 1=1 " +
            "            and m.deleted = 0 and m.member_type = '1' and m.status = '1' " +
            "            and m.member_account in (:staffs) ) " +
            "or f.flow_create_operator_account in (:staffs) " +
            "or f.system_admin_account in (:staffs)  " +
            " ) " +
            " ) " +
            "  " ,nativeQuery = true)
    List<FlowFormEvent> eventStatisticsView(@Param("start") LocalDateTime start, @Param("end") LocalDateTime end, @Param("flowCreateOperatorAccount") String flowCreateOperatorAccount, @Param("authCondition") FlowParamAuthDto authCondition, @Param("eventCause") List<Integer> eventCause, EventStatisticParamDto param, @Param("staffs") List<VEhrAdminStaff> staffs);
}
